package com.kehongborui.ticket.service.impl;

import com.kehongborui.ticket.entity.Ticket;
import com.kehongborui.ticket.mapper.TicketMapper;
import com.kehongborui.ticket.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhang peng yang ming
 * @CopyRight (C) http://www.csdn.com
 * @brief
 * @email 122162005@qq.com
 * @date 2018-08-15
 */
@Service
public class TicketServiceImpl implements TicketService {
    @Autowired
    private TicketMapper ticketMapper;

    /**
     * 根据id查找车票
     * @param id
     * @return
     */
    @Override //
    public Ticket findById(Long id) {
        return ticketMapper.selectByPrimaryKey(id);
    }

    /**
     * 新添加车票
     * @param ticket
     * @return
     */
    @Override
    public boolean create(Ticket ticket) {
        Integer result = ticketMapper.insert(ticket);
        return result > 0;
    }

    @Override
    public boolean create() {
        Ticket ticket = new Ticket();
        ticket.setAmount(1000);
        ticket.setEndStation("拉萨");
        ticket.setStartStation("成都");
        Integer result = ticketMapper.insert(ticket);
        int a = 1 / 0;

        ticket = new Ticket();
        ticket.setAmount(998);
        ticket.setEndStation("成都");
        ticket.setStartStation("拉萨");
        result = ticketMapper.insert(ticket);
        return true;
    }

    /**
     * 删除车票
     * @param id
     * @return
     */
    @Override
    public boolean delete(Long id) {
        int result = ticketMapper.deleteByPrimaryKey(id);
        return result > 0;
    }

    @Override
    public boolean update(Ticket ticket) {
        int result = ticketMapper.updateByPrimaryKeySelective(ticket);
        // 增加订单信息
        return result > 0;
    }

    @Override
    public List<Ticket> findAll() {
        return ticketMapper.findAll();
    }
}
